/*
 * parserenum.c
 *
 *  Created on: 2013-5-14
 *      Author: xkaying
 */

#include "parserenum.h"

#include "lexer.h"
#include "../typedef/token.h"
#include "../typedef/package.h"
#include "../typedef/import.h"
#include "../common/strutils.h"

#include <assert.h>

#include "parser.h"
//枚举变量的解析

int ParserEnum(Lexer *lexer, Package *package, int prority)
{
	assert(lexer);
	assert(package);
	int state;
	state = 0;
	ptoken = LexerGetToken(lexer);
	Enum *penum;
	penum = 0;
	char *name;
	int value;
	name = 0;
	value = 0;
	while (ptoken) {
		switch (state)
		{
			case 0: {
				//名字
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						state = 1;
						//
						penum = EnumInit(ptoken->name, prority);
						//将枚举变量加入到包中
						PackageAppendEnum(package, penum);
						//生成该包
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:枚举类型定义格式错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser import状态机错误.状态%d.\n", state);
				return 0;
			}
			case 1: {
				switch (ptoken->type)
				{
					case TokenType_BlockStart: {
						state = 2;
						//进入枚举内容读取
						ptoken = LexerGetToken(lexer);
						continue;
					}
						//表示枚举类型定义结束,定义了一个空枚举
					case TokenType_End: {
						printf("第%d行:定义了一个空枚举类型.\n", ptoken->line);
						ptoken = LexerGetToken(lexer);
						return 1;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:枚举类型定义格式错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser enum状态机错误.状态%d.\n", state);
				return 0;
			}
			case 2: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						state = 3;
						name = ptoken->name;
						//生成该包
						ptoken = LexerGetToken(lexer);
						continue;
					}
						//枚举定义结束
					case TokenType_BlockEnd: {
						ptoken = LexerGetToken(lexer);
						return 1;
					}
					case TokenType_End:
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:枚举类型内部数据项定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser enum状态机错误.状态%d.\n", state);
				return 0;
			}
			case 3: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_Equl: {
						state = 4; //读取枚举值
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_End: {
						ptoken = LexerGetToken(lexer);
						EnumAppend(penum, name, 0);
						state = 2;
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:枚举类型内部数据项定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser enum状态机错误.状态%d.\n", state);
				return 0;
			}
			case 4: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					//八进制
					case TokenType_Oct: {
						value = StrOct(ptoken->name);
						EnumAppend(penum, name, value);
						state = 5; //读取 ;号
						ptoken = LexerGetToken(lexer);
						continue;
					}
						//16进制
					case TokenType_Hex: {
						value = StrHex(ptoken->name);
						EnumAppend(penum, name, value);
						state = 5; //读取 ;号
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Decimal: {
						value = StrDecimal(ptoken->name);
						EnumAppend(penum, name, value);
						state = 5; //读取 ;号
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:枚举类型内部数据项定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser enum状态机错误.状态%d.\n", state);
				return 0;
			}
			case 5: {
				switch (ptoken->type)
				{
					case TokenType_End: {
						ptoken = LexerGetToken(lexer);
						state = 2;
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:枚举类型内部数据项定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser enum状态机错误.状态%d.\n", state);
				return 0;
			}
			default: {
				printf("parser enum状态机错误.状态:default.\n");
				return 0;
			}
		}
	}
	printf("不完整的枚举类型,是否缺少}结尾.\n");
	return 0;
}

